package org.colcon.web.model;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.colcon.web.model.framework.IDecision;
import org.colcon.web.model.framework.IPower;

public class Power extends Item implements IPower 
{
	private static final long serialVersionUID = -5401234057299294845L;

	private static final String
	
		// arguments
		CMD = "cmd",
		ARGS = "args";
	
	private String cmd;
	
	private Set<String> decisionArgs;
	
	public Power()
	{
		this(new HashMap<String, String>());
	}
	
	public Power(Map<String, String> args)
	{
		this(
				args.get(PARENT),
				args.get(ID),
				args.get(CMD),
				args.get(ARGS));
	}
	
	public Power(String parent, String id, String cmd, String decisionArgs)
	{
		this(parent, id, cmd, decisionArgs.split(" "));
	}
	
	public Power(String parent, String id, String cmd, String[] decisionArgs)
	{
		super(parent, id);
		
		this.cmd = cmd;
		
		this.decisionArgs = new HashSet<String>();
		
		for(String str : decisionArgs)
			this.decisionArgs.add(str);
	}

	public String getCmd()
	{
		return cmd;
	}
	
	public String[] getDecisionArgs()
	{
		return decisionArgs.toArray(new String[0]);
	}
	
	public String getText()
	{
		String str = cmd + " { ";
		
		for(String arg : decisionArgs)
			str += arg + " ";
		
		str += "}";
		
		return str;
	}
	
	public IDecision getDecision(Map<String, String> args)
	{
		String group = Model.parseGroup(getParentPath());
		
		String cmd = this.cmd + " -group " + group;
		
		for(String str : decisionArgs)
			if(args.containsKey(str))
				cmd += " " + CLI.ARG_TAG + str + " " + args.get(str);
		
		return new Decision(group, cmd);
	}
}
